package com.poem.controller;

import com.poem.dto.PoemsDTO;
import com.poem.dto.PoemsPageQueryDTO;
import com.poem.entity.Poems;
import com.poem.result.PageResult;
import com.poem.result.Result;
import com.poem.service.PoemsService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: PoemsService
 * @Author: YLY
 * @Description:
 * @Date: 2025/10/15
 * @Version: 1.0
 */
@RestController
@RequestMapping("/poems")
@Slf4j
public class PoemsController {
    // private static final Logger log =
    // LoggerFactory.getLogger(PoemsController.class);

    @Resource
    private PoemsService poemsService;

    @GetMapping("/list")
    public Result list() {
        log.info("查询所有故事");
        List<Poems> poemsList = poemsService.getAllPoems();
        return Result.success(poemsList);
    }

    /**
     * 添加古诗
     * 
     * @param poemsDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody PoemsDTO poemsDTO) {
        log.info("添加古诗:{}", poemsDTO);
        poemsService.addPoem(poemsDTO);
        return Result.success();
    }

    /**
     * 修改古诗
     * 
     * @param poemsDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody PoemsDTO poemsDTO) {
        log.info("修改古诗:{}", poemsDTO);
        poemsService.updatePoem(poemsDTO);
        return Result.success();
    }

    /**
     * 删除古诗
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除古诗:{}", ids);
        poemsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 分页查询
     * 
     * @param poemsPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult<Poems>> pageQuery(PoemsPageQueryDTO poemsPageQueryDTO) {
        log.info("分页查询：{}", poemsPageQueryDTO);
        PageResult<Poems> pageResult = poemsService.pageQuery(poemsPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/countByType")
    public Result<Map<Long, Integer>> countByType(@RequestParam("typeIds") List<Long> ids) {
        log.info("统计古诗数量");
        Map<Long, Integer> countMap = poemsService.countByType(ids);
        return Result.success(countMap);
    }

    @GetMapping("/{id}")
    public Result<Poems> getById(@PathVariable Long id) {
        log.info("查询id为{}的Poems", id);
        Poems poems = poemsService.getById(id);
        return Result.success(poems);
    }

}
